package com.hou.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hou.eduservice.client.VodClient;
import com.hou.eduservice.entity.EduVideo;
import com.hou.eduservice.mapper.EduVideoMapper;
import com.hou.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-17
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    // 删除小节
    @Override
    public void deleteVideoById(String id) {

        // 删视频
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();

        eduVideoQueryWrapper.eq("course_id",id);

        eduVideoQueryWrapper.select("video_source_id");

        List<EduVideo> videoList = baseMapper.selectList(eduVideoQueryWrapper);

        // 将list<eduvideo> 变成 list<string>
        List<String> finalList = new ArrayList<>();

        for (EduVideo video : videoList) {
            String videoSourceId = video.getVideoSourceId();
            finalList.add(videoSourceId);
        }


        // 远程调用vod方法，删除多个视频
        vodClient.removeMoreVideo(finalList);


        // 删小节
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("course_id",id);

        baseMapper.delete(queryWrapper);

    }


}
